package com.zg.common.core.enm;

import cn.hutool.core.collection.CollUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Zxb
 */
@AllArgsConstructor
@Getter
public enum ColumnTypeEnum {
    /**
     * 字段类型
     */
    LONG("长整数", "long", "bigint(20)", "NUMBER(20)", Long.class),
    INT("整数", "int", "int(11)", "NUMBER(11)", Integer.class),
    DOUBLE("浮点数", "double", "double", "NUMBER(10,2)", Double.class),
    STRING("字符串", "string", "varchar(255)", "NVARCHAR2(255)", String.class),
    TEXT("长文本", "text", "text", "NCLOB", String.class),
    BOOLEAN("布尔值", "boolean", "tinyint(1)", "NUMBER(4)", Boolean.class),
    DATE("日期", "date", "date", "DATE", Date.class),
    DATETIME("日期时间", "datetime", "datetime", "DATE", Timestamp.class),
    TIME("时间", "time", "time", "DATE", Time.class);
    private String name;
    private String code;
    private String mysqlType;
    private String oracleType;
    private Class javaType;

    public static String getMysqlType(String code) {
        ColumnTypeEnum[] arr = ColumnTypeEnum.values();
        List<ColumnTypeEnum> list = Arrays.stream(arr).filter(t -> t.code.equals(code)).collect(Collectors.toList());
        if (CollUtil.isEmpty(list)) {
            return "varchar(255)";
        }
        return list.get(0).getMysqlType();
    }

    public static String getOracleType(String code) {
        ColumnTypeEnum[] arr = ColumnTypeEnum.values();
        List<ColumnTypeEnum> list = Arrays.stream(arr).filter(t -> t.code.equals(code)).collect(Collectors.toList());
        if (CollUtil.isEmpty(list)) {
            return "NVARCHAR2(255)";
        }
        return list.get(0).getOracleType();
    }

    public static Class mysql2Java(String mysqlType) {
        ColumnTypeEnum[] arr = ColumnTypeEnum.values();
        List<ColumnTypeEnum> list = Arrays.stream(arr).filter(t -> t.mysqlType.startsWith(mysqlType)).collect(Collectors.toList());
        if (CollUtil.isEmpty(list)) {
            return Object.class;
        }
        return list.get(0).getJavaType();
    }

    public static Class oracle2Java(String oracleType) {
        ColumnTypeEnum[] arr = ColumnTypeEnum.values();
        List<ColumnTypeEnum> list = Arrays.stream(arr).filter(t -> t.oracleType.equals(oracleType)).collect(Collectors.toList());
        if (CollUtil.isEmpty(list)) {
            return Object.class;
        }
        return list.get(0).getJavaType();
    }
}
